package two.javaNow;

import two.diy.DispalyElement;

import java.util.Observable;
import java.util.Observer;

/**
 * 布告板
 */
public class CurrentConditionsDisplay implements Observer, DispalyElement {
    Observable observable;
    private float temperature;
    private float humidity;

    public CurrentConditionsDisplay(Observable observable) {//把此布告板注册到气象局
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void display() {//自己实现的展示数据布告板
        System.out.println("Current conditions: " + temperature
                + "F degrees and " + humidity + "% humidity");
    }

    @Override
    public void update(Observable obs, Object arg) {//气象局数据更新时就更新布告板数据
        if (obs instanceof WeatherData) {
            WeatherData weatherData = (WeatherData)obs;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            display();
        }
    }
}
